<?php
/*
 * Copyright (c) 2022.  Liuhongbin All rights reserved.
 * @domain http://dkyx.cc
 * @author liuhongbin <514660094@qq.com>
 */

namespace app\adminapi\controller\v1\customer;

use app\common\model\addon\Addons as AddonsModel;
use app\common\model\platform\Platform;
use app\common\services\User;
use think\App;
use think\facade\Route;

class Addons extends CustomerController
{
    public $notLogin = ['icon'];
    /**
     * @var AddonsModel
     */
    public $model;

    public function __construct(App $app, AddonsModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 获取应用列表
     * 可用应用、停用应用
     * @return \think\response\Json
     */
    public function index()
    {
        if (input('type', 0) == '0') {
            //已安装
            // 要获取当前平台下所有应用
            $search = input('search', []);
            $plaid = $search['plaid'] ?? 0;
            if ($plaid) {
                //前端获取应用
                $platform = (new Platform())->where(['plaid' => $search['plaid']])->find();
                $getUserAllAddons = (new User)->getUserAllAddons($platform['uid'], $plaid);
                $addon_names = [];
                foreach ($getUserAllAddons as $key => $value) {
                    $addon_names[] = $key;
                }
                $this->model->params['where']['name in'] = $addon_names;
                $this->model->params['where']['type'] = $platform['type'];
                unset($this->model->params['where']['plaid']);
            }

            // 获取应用所有者的应用的权限
            $res = $this->model->getPageList();
            $data = $res['data']->append(['type_name', 'new_version', 'auth', 'count_msg', 'used_platform_count'])->toArray();
            foreach ($data as $key => $value) {
                if ($value['new_version']) {
                    $data[$key]['is_update'] = $value['new_version'] != $value['version'] ? true : false;
                } else {
                    $data[$key]['is_update'] = false;
                }
                if ($plaid) {
                    //过滤部分权限
                    $data[$key]['auth'] = $this->removeAddonAuth($value['auth'], $getUserAllAddons[$value['name']]);
                }
            }
            return $this->success($res['count'], $data);
        } else {
            //未安装
            $installedAddons = $this->model->column('name');
            $addons = get_addon_list();
            $data = [];
            foreach ($addons as $key => $value) {
                if (in_array($value['name'], $installedAddons)) {
                    continue;
                }
                $data[] = array(
                    'author'    => $value['author'],
                    'name'      => $value['name'],
                    'title'     => $value['title'],
                    'status'    => $value['status'],
                    'version'   => $value['version'],
                    'type'      => $value['type'],
                    'type_name' => $this->model->_types[$value['type']],
                    'icon'      => Route::buildUrl("adminapi/addons/icon", ['name' => $value['name']])->domain(true)->build()
                );
            }
            return $this->success(count($data), $data, $this->model->params);
        }
    }


    protected function removeAddonAuth($auth, $userAuth)
    {
        $new_auth = [];
        if (!in_array("all", $userAuth)) {
            foreach ($auth as $k => $v) {
                if (isset($v['children'])) {
                    $new_auth[] = [
                        'key'      => $v['key'],
                        'label'    => $v['label'],
                        'children' => $this->removeAddonAuth($v['children'], $userAuth)
                    ];
                } else {
                    if (in_array($v['key'], $userAuth)) {
                        $new_auth[] = $v;
                    }
                }
            }
        } else {
            $new_auth = $auth;
        }
        return $new_auth;
    }
}
